package com.sailorj.medical.web.servlet;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sailorj.medical.domain.area.Area;
import com.sailorj.medical.domain.family.Family;
import com.sailorj.medical.domain.paybase.PayBase;

public class PayBaseServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m = request.getParameter("m");// 动作类型参数
		if ("list".equals(m)) {
			process(request, response, "/page/payBase/payBase_list.jsp");
		} else if ("input".equals(m)) {
			process(request, response, "/page/payBase/payBase_add.jsp");
		} else if ("get".equals(m)) {
			process(request, response, "/page/payBase/payBase_edit.jsp");
		} else if ("add".equals(m)) {
			add(request, response);
			process(request, response, "/page/payBase/payBase_list.jsp");
		} else if ("edit".equals(m)) {
			this.edit(request, response);
		} else if ("del".equals(m)) {
			this.del(request, response);
		}
	}


	private void add(HttpServletRequest rs, HttpServletResponse response) throws ServletException, IOException {
		// 接收页面传递过来的参数
		String money = rs.getParameter("money");
		String startTime1 = rs.getParameter("startTime");
		String endTime1 = rs.getParameter("endTime");
		Timestamp startTime = null;
		Timestamp endTime = null;
		startTime = Timestamp.valueOf(startTime1);
		endTime = Timestamp.valueOf(endTime1);

		try {
			// 2:构造新慢病信息对象
			PayBase model = new PayBase();

			// 3：调用保存的方法
			model.add(money,startTime,endTime);
			process(rs, response, "/page/payBase/payBase_list.jsp");
		} catch (Exception e) {
			error(rs, response);
		}

	}

	private void edit(HttpServletRequest rs, HttpServletResponse resp) throws ServletException, IOException {

		// 1：接收参数

		String id = rs.getParameter("id");
		String year = rs.getParameter("year");
		String money = rs.getParameter("money");
		String startTime1 = rs.getParameter("startTime");
		String endTime1 = rs.getParameter("endTime");
		Timestamp startTime = null;
		Timestamp endTime = null;
		startTime = Timestamp.valueOf(startTime1);
		endTime = Timestamp.valueOf(endTime1);
		try {
			// 2:构造新慢病信息对象
			PayBase model = new PayBase(id, year, money, startTime, endTime);

			// 3：调用保存的方法
			model.edit();
			process(rs, resp, "/page/payBase/payBase_list.jsp");
		} catch (Exception e) {
			error(rs, resp);
		}

	}

	private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 1：接收参数
		String id = req.getParameter("id");

		// 2:构造新慢病信息对象
		PayBase model = new PayBase();
		try {
			model.del(id);
			process(req, resp, "/page/payBase/payBase_list.jsp");
		} catch (Exception e) {
			error(req, resp);
		}

	}

}
